<?php
class Home extends Controller {

	private $_view_data;
	private $_current_user_id;
	private $_current_user_data;
	private $_is_community_member;
	
	function Home() {
		
		parent::Controller();
		
//		$this->output->enable_profiler(TRUE);
		
		$enabled = $this->config->item('Enabled');
		if(!$enabled) {
			show_error('OoW = Out of Work!');
		}
		
		$this->load->library('auth');
		$this->load->library('common');
		$this->load->library('cache');
		$this->load->library('user_agent');
		
		$this->load->model('Identity_Manager');
		$this->load->model('Blogger_Manager');
		
		// check identity
		$this->Identity_Manager->DoIdentityCheck(); // special for homepage
		$this->_is_community_member = $this->Identity_Manager->IsCommunityMember();
		$this->_current_user_id = $this->Identity_Manager->CurrentUserID();
		$this->_current_user_data = $this->Identity_Manager->CurrentUserData();
		$this->_view_data['is_community_member'] = $this->_is_community_member;
		$this->_view_data['current_user_id'] = $this->_current_user_id;
		if($this->_current_user_id > 0) {
			$this->_view_data['current_user_data'] = $this->_current_user_data;
			// get pms
			$this->load->model('Message_Manager');
			$this->Message_Manager->SetCurrentBloggerID($this->_current_user_id);
			$unread_pm_num = $this->Message_Manager->GetUnreadPMNum();
			$this->_view_data['unread_pm_num'] = $unread_pm_num;
		}
		
		// Language
		$this->lang->load('n8_home');
		
		$ie_warning = FALSE;
		// ie-warning
		if($this->agent->is_browser()) {
			if(strpos(strtolower($this->agent->browser()), 'internet explorer') !== FALSE) {
				$ie_warning = TRUE;
			}
		}
		$this->_view_data['ie_warning'] = $ie_warning;
	}
	
	function index() {
		$bloggers = $this->cache->Load('blogger_list');
		if(!$bloggers) {
			$bloggers = $this->Blogger_Manager->GetBloggerList();
			$this->cache->Store('blogger_list', json_encode($bloggers));
		} else {
			$bloggers = json_decode($bloggers);
		}
		$this->_view_data['bloggers'] = $bloggers;
		
		$latest_entries = $this->cache->Load('latest_entries');
		if(!$latest_entries) {
			$latest_entries = $this->Blogger_Manager->GetLatestEntries(7); // why 7 -_-?
			$this->cache->Store('latest_entries', json_encode($latest_entries));
		} else {
			$latest_entries = json_decode($latest_entries);
		}
		$this->_view_data['latest_entries'] = $latest_entries;
		
		$ann_fp = $_SERVER['DOCUMENT_ROOT'] . '/data/home_ann.txt';
		$home_ann = $this->common->UBBCode(file_get_contents($ann_fp));
		$this->_view_data['home_ann'] = $home_ann;
		
		$avatar_refresh = FALSE;
		$avatar_version = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/data/avatar_ver.txt');
		$client_avatar_version = $this->common->GetCookie('avatar_version');
		if($client_avatar_version === FALSE) {
			$avatar_refresh = TRUE;
		} else {
			if((int)$client_avatar_version < (int)$avatar_version) {
				$avatar_refresh = TRUE;
			}
		}
		if($avatar_refresh) {
			$this->common->SetCookie('avatar_version', $avatar_version, 28); // for 4 weeks
		}
		$this->_view_data['avatar_refresh'] = $avatar_refresh;
		
		$agent_is_ipod = $this->agent->is_mobile();
		if($agent_is_ipod) {
			$this->load->view('mobile/home_view', $this->_view_data);
		} else {
			$this->load->view('home_view', $this->_view_data);
		}
	}
	
	function search() {
		echo('// TODO');
	}
	
	function rss() {
		$rss_entries = $this->cache->Load('latest_entries');
		if(!$rss_entries) {
			$rss_entries = $this->Blogger_Manager->GetLatestEntries(7); // why 7 -_-?
			$this->cache->Store('latest_entries', json_encode($rss_entries));
		} else {
			$rss_entries = json_decode($rss_entries);
		}
		foreach($rss_entries as $re) {
			$re->content = $this->common->UBBCode($re->content);
		}
		$this->_view_data['rss_entries'] = $rss_entries;
		
		$this->output->set_header('Content-Type: text/xml; charset=utf-8');
		$this->load->view('home_rss_view', $this->_view_data);
	}
	
	function emotags_list($page_no = 0) {
		$per_page = 7; // how many to show?
		$total_emotags = $this->Blogger_Manager->CountAllEmoTags();
		$total_pages = ceil($total_emotags / $per_page);
		$prev = $page_no - 1;
		if($page_no == 0) {
			$prev = 0;
		}
		$next = $page_no + 1;
		if($next == $total_pages) {
			$next = $total_pages - 1;
		}
		$this->_view_data['prev_string'] = $prev;
		$this->_view_data['next_string'] = $next;
		$this->_view_data['paging_info'] = (string)($page_no + 1) . '/' . (string)$total_pages;
		
		$emotags = $this->Blogger_Manager->ListAllEmoTags($per_page, $page_no);
		// proc for friendly time
		$now_time = $this->common->Now();
		foreach($emotags as $em) {
			$em->post_time = $this->common->TimeDiff($em->post_time, $now_time);
		}
		$this->_view_data['emotags'] = $emotags;
		
		$this->load->view('home_emotags_list_view', $this->_view_data);
	}
	
	// this is an experiment feature integrated with imifield.com
	function im() {
		$this->load->model('EmoTag_Manager');
		
		$user_key = $this->input->post('userkey');
		$msg = $this->input->post('msg');
		$step = $this->input->post('step');
		$ret = 'NO-CODE<reset>';
		if($step == 1) {
			if($msg == '/reg' || $msg == 'addme') {
				$ret = 'Please enter your *emoTag-IM-Link-Code* (See your Blog Dashboard):';
			} else { // emotag content
				$msgs = explode(' ', $msg, 2);
				$oper = strtolower($msgs[0]);
				if($oper == '/post' || $oper == 'emotag') {
					$emo_text = $msgs[1];
					$r = $this->EmoTag_Manager->IM_PostEmoTag($user_key, $emo_text);
					if($r === TRUE) {
						$ret = 'OK, your emoTag was published!<reset>';
					} else {
						$ret = 'Publishing failed! Have you registered?<reset>';
					}
				} else {
					$ret = 'Wrong code!<br>1. To link your IM with your account, type "/reg".<br>2. To post, enter "/post Your-Content".<reset>';
				}
			}			
		} elseif($step == 2) {
			$msgs = explode("_", $msg);
			if(!is_array($msgs)) {
				$ret = 'Input error (whole code), please retry.<reset>';
			} else {
				$blogger_id = $msgs[0];
				if(!is_numeric($blogger_id)) {
					$ret = 'Input error (blogger-id), please retry.<reset>';
				} else {
					$pwd_hash = $msgs[1];
					$t = $this->auth->Login($blogger_id, $pwd_hash, TRUE);
					if($t === FALSE) {
						$ret = 'Linking IM-user failed, double check your input and try again.<reset>';
					} else {
						$r = $this->EmoTag_Manager->IM_AddUser($blogger_id, $user_key);
						if($r === FALSE) {
							$ret = 'Linking IM-user failed, you may have already linked before?<reset>';
						} else {
							$ret = 'Linking IM-user successful! Now you can post emoTags via IM, enjoy!<reset>';
						}
					}
				}
			}
		}
		
		header('Content-Type: text/plain; charset=utf-8');
		echo($ret);
	}
}
?>